define(function (require, exports, module) {
    /**--tmpl-require--**/

        //加载依赖

    require("ajaxproxy");
    require("JTemplate");
    require("layer");

    var common = require("common");

    //初始化AjaxProxy插件
    $('.ajaxproxy').AjaxProxy({
        dataType: "json",
        method: "post",
        formId: "content-add-form",
        formCheckHandler: function (form_id) {
            return common.formCheckHandler(form_id, true);
        },
        callbackDelay: __global.jdialog.timer,
        timeInterval: __global.jdialog.timer,
        callBack: function (data) {   /* 执行ajax之后的回调函数 */
            common.ajaxCallback(data);
        }
    });

    exports.init = function () {

        $("select[name='data[tmpl_id]']").change(function () {

            var obj = $(this).find("option:selected");

            $.get('getRemindTmplById?id=' + obj.val(), function (data) {
                if (data.data.id) {

                    $("textarea[name='data[to_content]']").text(data.data.content);
                }else{

                    $("textarea[name='data[to_content]']").text('');
                }
            }, 'json');


        });

        $("a.js-tag").click(function () {
            insertAtCursor(document.getElementById('content'), $(this).find('span').text());


        });



    };
});

//光标处插入字符
function insertAtCursor(myField, myValue) {
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        sel.select();
    }
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        var restoreTop = myField.scrollTop;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        if (restoreTop > 0) {
            myField.scrollTop = restoreTop;
        }
        myField.focus();
        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
    } else {
        myField.value += myValue;
        myField.focus();
    }
}